package org.millioncall.yueyoga.admin.dao.impl;

import java.util.List;
import java.util.Map;

import org.apache.ibatis.session.RowBounds;
import org.millioncall.openplatform.common.PaginateParams;
import org.millioncall.openplatform.common.PaginatedList;
import org.millioncall.openplatform.common.StartLimitPairImpl;
import org.millioncall.openplatform.dao.PreparedQuery;
import org.millioncall.openplatform.util.StringUtils;
import org.millioncall.yueyoga.admin.dao.BaseDao;
import org.millioncall.yueyoga.admin.dao.GoodsDAO;
import org.millioncall.yueyoga.admin.model.AviatorExpression;
import org.millioncall.yueyoga.admin.model.Goods;
import org.millioncall.yueyoga.admin.service.advancedsearch.AviatorResultHandler;
import org.springframework.stereotype.Repository;

/**
 * @author lisj Mar 27, 2014 2:26:15 PM
 */
@Repository
public class GoodsDAOImpl extends BaseDao implements GoodsDAO {

	private static final String SAVE_GOODS = "saveGoods";
	private static final String UPDATE_GOODS_BY_ID = "updateGoodsById";
	private static final String DELETE_GOODS_BY_ID = "deleteGoodsById";
	private static final String FIND_GOODS_BY_ID = "findGoodsById";
	private static final String FIND_GOODS_BY_CONDITIONS = "findGoodsByConditions";
	private static final String FIND_NEXTCODE = "findNextCode";

	@Override
	public int save(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().insert(SAVE_GOODS, map);
	}

	@Override
	public int update(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().update(UPDATE_GOODS_BY_ID,
				map);
	}

	@Override
	public int deleteById(Integer id) {
		return this.getPagableSqlSessionTemplate().delete(DELETE_GOODS_BY_ID,
				id);
	}

	@Override
	public int deleteByIds(String[] ids) {
		int len = 0;
		for (String id : ids) {
			len = deleteById(Integer.valueOf(id));
		}
		return len;
	}

	@Override
	public Goods findGoodsById(Integer id) {
		return this.getPagableSqlSessionTemplate().selectOne(FIND_GOODS_BY_ID,
				id);
	}

	@Override
	public PaginatedList<Goods> findGoodsInPage(Map<String, Object> map,
			int start, int limit) {
		PreparedQuery<Goods> pq = this.getPagableSqlSessionTemplate().findAll(
				FIND_GOODS_BY_CONDITIONS, map);
		PaginatedList<Goods> pl = pq.list(new PaginateParams(
				new StartLimitPairImpl(start, limit)));
		return pl.size() > 0 ? pl : null;
	}

	@Override
	public String getNextCode() {
		String maxCode = this.getPagableSqlSessionTemplate().selectOne(
				FIND_NEXTCODE);
		// --code exp:YJSP0000022
		String currentCode = "";
		String splitStr = "YJSP";
		if (StringUtils.isBlank(maxCode)) {
			currentCode = splitStr + "0000001";
		} else {
			maxCode = maxCode.split(splitStr)[1];
			Integer code = Integer.valueOf(maxCode);
			if (code < 9) {
				code++;
				currentCode = splitStr + "000000" + code;
			} else if (code == 9 || code < 99) {
				code++;
				currentCode = splitStr + "00000" + code;
			} else if (code == 99 || code < 999) {
				code++;
				currentCode = splitStr + "0000" + code;
			} else if (code == 999 || code < 9999) {
				code++;
				currentCode = splitStr + "000" + code;
			} else if (code == 9999 || code < 99999) {
				code++;
				currentCode = splitStr + "00" + code;
			} else if (code == 99999 || code < 999999) {
				code++;
				currentCode = splitStr + "0" + code;
			} else if (code == 999999 || code < 9999999) {
				code++;
				currentCode = splitStr + code;
			}
		}
		return currentCode;
	}

	@Override
	public List<Goods> findAll(Map<String, Object> map) {
		return this.getPagableSqlSessionTemplate().selectList("findAllGoods",map);
	}

	@Override
	public Goods findGoodsByCode(String code) {
		return this.getPagableSqlSessionTemplate().selectOne("findGoodsByCode",code);
	}

	@Override
	public int updateGoodCounts(Map<String, Object> context) {
		return this.getPagableSqlSessionTemplate().update("updateGoodCounts",
				context);
	}

	@Override
	public List<Goods> findAllByOrganizationId(String organizationId) {
		return this.getPagableSqlSessionTemplate().selectList("findAllByOrganizationId",organizationId);
	}

	@Override
	public List<Goods> getAllGoodsWithAviator(Map<String, Object> params,
			AviatorExpression expression, Map<String, Object> aviatorParams,
			int limit) {
		AviatorResultHandler<Goods> handler = new AviatorResultHandler<Goods>(
				expression, aviatorParams, limit);
		this.getPagableSqlSessionTemplate().select(FIND_GOODS_BY_CONDITIONS, params,
				new RowBounds(0, Integer.MAX_VALUE), handler);
		return handler.getResultList();
	}

}
